<template>
  <div>
    <Panel>
      <span slot="title">账号列表</span>
      <div slot="main">
        <!-- 表单 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 1000px"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>

          <el-table-column label="账号">
            <template slot-scope="scope">{{ scope.row.account }}</template>
          </el-table-column>

          <el-table-column label="用户组">
            <template slot-scope="scope">{{ scope.row.userGroup }}</template>
          </el-table-column>

          <el-table-column label="创建时间">
            <template slot-scope="scope">{{
              scope.row.ctime | normalDate
            }}</template>
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定需要删除吗?"
                @confirm="handleDelete(scope.row.id)"
              >
                <el-button slot="reference" size="mini" type="danger"
                  v-permission="['super', 'normal']">删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
          <el-form :model="accForm">
            <el-form-item label="账号" label-width="100px">
              <el-input v-model="accForm.account" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="用户组" label-width="100px">
              <el-select v-model="accForm.userGroup" placeholder="请选择用户组">
                <el-option label="普通用户" value="普通管理员"></el-option>
                <el-option label="超级用户" value="超级管理员"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[1, 3, 5, 7]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>

        <el-button type="danger" @click="dels">批量删除</el-button>
        <el-button type="primary" @click="toggleSelection()"
          >取消选择</el-button
        >
      </div>
    </Panel>
  </div>
</template>

<script>
import Panel from "@/components/panel.vue";
import { listApi, delApi, delsApi, editApi } from "@/api/user";
export default {
  components: {
    Panel,
  },
  data() {
    return {
      // 表单
      tableData: [],

      // 分页
      currentPage: 1,
      pageSize: 1,
      total: 0,

      ids: [],
      dialogFormVisible: false,
      accForm: {
        account: "",
        userGroup: "",
      },
    };
  },
  //
  created() {
    this.getData();
  },

  methods: {
    // 数据渲染
    async getData() {
      let res = await listApi({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      let { data, total } = res.data;
      if (!data.length && this.currentPage != 1) {
        this.currentPage -= 1; //回拉一页
        this.getData(); //重新渲染数据
      }
      this.tableData = data;
      this.total = total;
    },

    // 删除
    async handleDelete(id) {
      // console.log(id);
      let res = await delApi({ id });
      //console.log(res);
      let { code, msg } = res.data;
      if (code == 0) {
        // this.$message.success(msg);
        this.getData();
      }
    },

    // 页脚
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },

    // 表单取消全选
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          //console.log(this.$refs.multipleTable.toggleRowSelection(row));
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },

    //将表单里选择的选项中的id遍历到ids
    handleSelectionChange(val) {
      this.ids = val.map((v) => v.id);
    },

    // 批量删除
    dels() {
      if (this.ids.length) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            // 发请求
            // console.log(this.ids);
            let res = await delsApi({ ids: this.ids });
            // console.log(res);
            let { code, msg } = res.data;
            if (code == 0) {
              // this.$message.success(msg);
              this.getData();
            }
          })
          // .catch(() => {
          //   this.$message({
          //     type: "info",
          //     message: "已取消删除",
          //   });
          // });
      } else {
        this.$message.error("请选择你需要删除");
      }
    },

    handleEdit(row) {
      this.dialogFormVisible = true;
      this.accForm = { ...row };
    },

    async edit() {
      let res = await editApi({
        id: this.accForm.id,
        account: this.accForm.account,
        userGroup: this.accForm.userGroup,
      });
      let { code, msg } = res.data;
      // if (code == 0) {
      //   this.$message.success(msg);
      // }
      this.dialogFormVisible = false;
      this.getData();
    },
  },
};
</script>

<style lang="less" scoped>
.el-pagination {
  margin: 30px 0px;
}
.el-input {
  width: 206px;
}
/deep/.el-dialog {
  width: 500px;
}
.el-button--default {
  margin-right: 20px;
}
</style>